<?php

namespace app\api\services\merchant;

use app\api\model\Goods;
use app\api\model\JoinMerchant;
use app\api\services\BaseServices;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;

class MerchantServices extends BaseServices
{
    /**
     * 商家详情
     * @param $id
     * @param $goodsName
     * @return array
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public static function detail($id,$goodsName): array
    {
        $merchant = JoinMerchant::field('id,trade_name,address,avatar,star,status')->where('id', $id)->find();
        if (empty($merchant) || $merchant->status != 2) {
            throw new Exception("该商户不存在");
        }

        // 销量
        $salesVolume = Goods::where('join_merchant_id', $id)->sum('sales');
        [$page, $limit] = self::getPageValue();
        $where = [];
        $where['join_merchant_id'] = ['=',$id];
        if (!empty($goodsName)){
            $where['name']=['like',"%$goodsName%"];
        }
        $goods = Goods::where($where)->paginate([
            'list_rows' => $limit,
            'page' => $page
        ]);
        return [
            'merchant' => $merchant,
            'salesVolume' => $salesVolume,
            'page' => $goods->currentPage(),
            'limit' => $goods->listRows(),
            'total' => $goods->total(),
            'goodsList' => $goods->items(),
        ];
    }
}